﻿Ext.define('app.prm.project.Edit',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    width: 850,
    height: 600,
    layout: 'fit',
    initComponent: function () {
        var me = this;
        me.items = [
            {
                xtype: 'container',
                anchor: '100%',
                autoScroll: true,
                layout: 'hbox',
                items: [
                    {
                        xtype: 'container',
                        flex: 2,
                        layout: 'anchor',
                        items: [me.getForm()]
                    },
                    {
                        xtype: 'container',
                        flex: 1,
                        bodyStyle: 'padding:5px',
                        items: [me.getPhaseTree()]
                    }
                ]
            }
        ];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            App.AjaxGet('/api/services/app/user/Get', function (data) {
                var cbo_tree = me.getComboTree();
                var cboValue = "";
                if (data.result.user.businessId > 0) {
                    cboValue = data.result.businessName;
                }
                if (data.result.user.rdCenterId > 0) {
                    cboValue = data.result.rdCenterName;
                }
                if (data.result.user.laboratoryId > 0) {
                    cboValue = data.result.laboratoryName;
                }


                cbo_tree.setRawValue(cboValue);
                cbo_tree.BusinessId = data.result.user.businessId;
                cbo_tree.RdCenterId = data.result.user.rdCenterId;
                cbo_tree.LaboratoryId = data.result.user.laboratoryId;
            });
        }
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                defaults: { anchor: '90%' },
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        hidden: true,
                        name: 'Id',
                        value: 0
                    },
                    {
                        xtype: 'container',
                        anchor: '100%',
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'displayfield',
                                        allowBlank: false,
                                        fieldLabel: '项目编号',
                                        value: '自动生成'
                                    },
                                      {
                                          xtype: 'combo',
                                          reference: 'CustomerCombo',
                                          name: 'Customer',
                                          allowBlank: false,
                                          fieldLabel: '客户名称',
                                          emptyText: '输入姓名查询',
                                          store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetCustomers', autoLoad: false }),
                                          queryParam: 'name',
                                          typeAhead: false,
                                          minChars: 1,
                                          queryMode: 'remote',
                                          triggerAction: 'query',
                                          displayField: 'name',
                                          valueField: 'id'
                                      },

                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: '质量标准',
                                        name: 'QualityStandard'
                                    },

                                    {
                                        xtype: 'textfield', hidden: true,
                                        fieldLabel: '计划人力成本',
                                        name: 'TargetWorkCost'
                                    },
                                    {
                                        xtype: 'datefield',
                                        editable: false,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        fieldLabel: '要求完成时间',
                                        value: Ext.Date.add(new Date(), Ext.Date.MONTH, 1),
                                        name: 'TargetEndDate'
                                    }, {
                                        xtype: 'combo',
                                        maxHeight: 100,
                                        //id: 'cbo_principal',
                                        name: 'productNo',
                                        allowBlank: false,
                                        fieldLabel: '产品号',
                                        emptyText: '输入产品号查询',
                                        store: Ext.create("app.ux.PagePrmStore", { url: '/api/services/app/product/getall', autoLoad: false }),
                                        queryParam: 'code',
                                        typeAhead: false,
                                        minChars: 1,
                                        queryMode: 'remote',
                                        triggerAction: 'query',
                                        displayField: 'code',
                                        valueField: 'id'
                                    },
                                    {
                                        xtype: 'combo',
                                        reference: 'PMId',
                                        maxHeight: 100,
                                        //id: 'cbo_principal',
                                        name: 'PMId',
                                        allowBlank: false,
                                        fieldLabel: '项目经理',
                                        emptyText: '输入姓名查询',
                                        store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetUsers', autoLoad: false }),
                                        queryParam: 'name',
                                        typeAhead: false,
                                        minChars: 1,
                                        queryMode: 'remote',
                                        triggerAction: 'query',
                                        displayField: 'name',
                                        valueField: 'id'
                                    },

 me.getComboTree()
                                ]
                            },
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        allowBlank: false,
                                        fieldLabel: '项目名称',
                                        name: 'ProjectName'
                                    }, {
                                        xtype: 'textfield',
                                        fieldLabel: '项目性质',
                                        hidden: true,
                                        name: 'ProjectNature'
                                    },
                                    {
                                        xtype: 'combo',
                                        fieldLabel: '项目任务',
                                        name: 'TaskType',
                                        value: '小试',
                                        hidden: true,
                                        editable: false,
                                        store: ['小试', '中试', '其它']
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: '中试地点',
                                        name: 'Address'
                                    },

                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: '目标完成量',
                                        name: 'TargetOutPut'
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: '目标项目成本',
                                        name: 'TargetCost'
                                    },
                                    {
                                        xtype: 'textfield', hidden: true,
                                        fieldLabel: '目标原料成本',
                                        name: 'TargetMaterialCost'
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '客户编号',
                                        name: 'CustomeCode'
                                    },
                                   {
                                       xtype: 'radiogroup', 
                                       fieldLabel: '客户周报', columnWidth: 0.7,
                                       items: [
                                           { boxLabel: '单周报', name: 'ReportWeek', inputValue: '1',checked:true },
                                           { boxLabel: '双周报', name: 'ReportWeek', inputValue: '2' }
                                       ]
                                   },
                                    {
                                        xtype: 'datefield',
                                        editable: false,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        fieldLabel: '计划开始时间',
                                        value: new Date(),
                                        name: 'PlanStartDate'
                                    },
                                    {
                                        xtype: 'datefield',
                                        editable: false,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        value: Ext.Date.add(new Date(), Ext.Date.MONTH, 1),
                                        fieldLabel: '计划结束时间',
                                        name: 'PlanEndDate'
                                    }
                                ]
                            }
                        ]
                    },
                    //{
                    //    xtype: 'tagfield',
                    //    anchor: '85%',
                    //    //id: 'txt_Phase',
                    //    fieldLabel: '项目阶段',
                    //    name: 'PhaseSelect',
                    //    allowBlank: false,
                    //    store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/phaseInfo/GetPhaseInfo' }),
                    //    valueField: 'id',
                    //    displayField: 'name'
                    //},
                    {
                        xtype: 'htmleditor',
                        height: 210,
                        fieldLabel: '备注',
                        anchor: '80%',
                        name: 'Remark',
                        value: '目标<div>要求</div>'
                    }
                ]
            });
        }
        return this.form;
    },
    getPhaseTree: function () {
        var me = this;
        if (!this.phaseTree) {
            this.phaseTree = new Ext.tree.Panel({
                border: false,
                useArrows: true,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/project/GetProjectPhaseCheckTree'
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) { tree.expandAll(); },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    },
                    cellclick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                        if (cellIndex == 0) {
                            record.set('checked', !record.data.checked);;
                            if (record.data.checked) {
                                record.eachChild(function (child) {
                                    me.checkChild(child, true);
                                });
                            }
                            else {
                                record.eachChild(function (child) {
                                    me.checkChild(child, false);
                                });
                            }
                            me.checkParent(record);// 进行父级选中操作
                        }
                    }
                }
            });
        }
        return this.phaseTree;
    },
    checkChild: function (node, check) {
        var me = this;
        node.set('checked', check);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkChild(child, check);
            });
        }
    },
    nodep: function (node) {
        var bnode = false;
        Ext.Array.each(node.childNodes, function (v) {
            if (v.data.checked) {
                bnode = true;
                return;
            }
        });
        return bnode;
    },
    checkParent: function (node) {
        var me = this;
        if (node.parentNode != null) {
            if (me.nodep(node.parentNode)) {
                node.parentNode.set('checked', true);
            } else {
                node.parentNode.set('checked', false);
            }
            me.checkParent(node.parentNode);
        }
    },
    getComboTree: function () {
        if (!this.comboTree) {
            this.comboTree = Ext.create("app.ux.ComboTreeBox", {
                //id: 'cbo_tree',
                fieldLabel: '负责科室',
                editable: false,
                store: new Ext.data.TreeStore({
                    root: {
                        expanded: true
                    },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/laboratory/GetTree'
                    }
                })
            });
        }
        return this.comboTree;
    },
    onSubmit: function () {
        var me = this;

        var arry = me.getPhaseTree().getChecked();
        var phases = [];
        for (var i = 0; i < arry.length; i++) {
            var node = arry[i];
            if (node.data.id != "0") {
                phases.push(parseInt(node.data.id));
            }
        }

        if (!me.getForm().getForm().isValid()) {
            return false;
        }
        var cbo_tree = me.getComboTree();
        // var txt_Phase = me.form.query("tagfield[name=PhaseSelect]")[0].getValue();
        var json = me.getForm().getForm().getValues();
        json["PhaseSelect"] = phases;
        json["BusinessId"] = cbo_tree.BusinessId;
        json["RdCenterId"] = cbo_tree.RdCenterId;
        json["LaboratoryId"] = cbo_tree.LaboratoryId;
        json["PMId"] = me.form.query("combo[name=PMId]")[0].getValue();
        json["CustomerId"] = me.form.query("combo[name=Customer]")[0].getValue();
        json['Customer'] = me.form.query("combo[name=Customer]")[0].getRawValue();

        var productCombo = me.form.query("combo[name=productNo]")[0];
        json["ProductNo"] = productCombo.getRawValue();
        json['ProductId'] = productCombo.getValue();

        if (json["PMId"] == '' || isNaN(json["PMId"])) {
            alert("请通过选择录入项目经理");
            return false;
        }

        if (json["CustomerId"] == '' || isNaN(json["CustomerId"])) {
            alert("请通过选择录入客户");
            return false;
        }
        if (json["CustomerId"] == '' || isNaN(json["CustomerId"])) {
            alert("请通过选择录入客户");
            return false;
        }
        if (json["ProductId"] == '' || isNaN(json["ProductId"])) {
            alert("请通过选择录入产品号");
            return false;
        } 


        App.AjaxPost('/api/services/app/project/CreateProject', json, function () {
            me.fireEvent("savesuccess");
        });
    }
});